package com.company.ecis.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.company.ecis.entity.BindingCard;
import com.company.ecis.entity.Userinfo;
import com.company.ecis.entity.vo.BindingCardVO;
import com.company.ecis.service.BindingCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/ecis/binding-card")
public class BindingCardController {

    @Autowired
    private BindingCardService bindingCardService;


    @GetMapping("/getCardSet")
    public String getCardSetByUserId(int userId, HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        HttpSession session = request.getSession();
        Userinfo userInfo = (Userinfo) session.getAttribute(String.valueOf(userId));
        List<BindingCard> cardList= bindingCardService.selectCardsByUsername(userInfo.getUsername());
        if (cardList == null){
            jsonObject.set("code",403).set("msg","尚未绑定");
            return JSONUtil.toJsonStr(jsonObject);
        }
        jsonObject.set("code",200).set("msg","获取成功");
        List<BindingCardVO> bindingCardVOList = new ArrayList<>();
        for (BindingCard cardCard : cardList){
            BindingCardVO bindingCardVO = new BindingCardVO();
            BeanUtil.copyProperties(cardCard,bindingCardVO);
            bindingCardVOList.add(bindingCardVO);
        }
        jsonObject.set("data",bindingCardVOList);
        System.out.println(bindingCardVOList);
        return JSONUtil.toJsonStr(jsonObject);
    }


    @PostMapping("/binding-card")
    public String bindCard(String password, String cardId, HttpSession session){
        return bindingCardService.bindCard(password,cardId,session);
    }


}

